/// @tags:
#include <cstdio>
#include <functional>
#include <iostream>
#include <numeric>

using std::cin, std::cout, std::endl;

namespace BlueQuantum {

constexpr int maxn = 50;

int a[maxn + 1][maxn + 1];
int b[maxn + 1][maxn + 1];

bool vis[maxn + 1][maxn + 1];

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    for (int j = 1; j <= n; ++j) {
      cin >> a[i][j];
      b[a[i][j]][i] = j;
    }
  }

  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      int p = b[i][j];
      if (p > 1) vis[i][a[j][p - 1]] = true;
      if (p < n) vis[i][a[j][p + 1]] = true;
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    int cnt = 0;
    for (int j = 1; j <= n; ++j) {
      if (i == j) continue;
      if (!vis[i][j]) ++cnt;
    }
    ans += cnt;
  }
  cout << ans / 2;
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("B - Discord.in", "r", stdin);
  freopen("B - Discord.out", "w", stdout);
#endif
#endif

  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
